using System;
using System.Threading;
using DaaseVarmer.helperClasses;
using DaaseVarmer.helperClasses.HardwareInterface;
using DaaseVarmer.helperClasses.networkComponents;
using Microsoft.SPOT;

namespace DaaseVarmer.domain
{
    //todo needs to be mate thread safe
    public class LogManager
    {
        private static LogManager instance;
        //private SdAccess sdAccess { get; set; }
        private PachubeClient pachubeClient { get; set; }
        private Settings Settings = Settings.Instance;

        private LogManager()
        {
        }

        public static LogManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new LogManager();
                }
                return instance;
            }
        }

        /// <summary>
        /// Logs a program error to the sd card
        /// </summary>
        /// <param name="error"></param>
        public  void Error(string error) 
        {
            //sdAccess = new SdAccess();
            //sdAccess.Write(WriteType.Error, error);
        }

        /// <summary>
        /// Logs a program event to the sd card
        /// </summary>
        /// <param name="ProgramEventToSD"></param>
        public void ProgramInfo(string ProgramEventToSD)
        {
            //sdAccess = new SdAccess();
            //sdAccess.Write(WriteType.Info, ProgramEventToSD);
        }

        /// <summary>
        /// Logs atemperatur to sd and pachube
        /// </summary>
        /// <param name="dataID">wich temperatur sensor that sends the data</param>
        /// <param name="data">temperatur</param>
        public void LogData(WriteType writetype, string data)
        {
            //sdAccess = new SdAccess();
            bool isNetworkEnabled = false;
            string pachubeFeedStream = "";
           
            switch(writetype){
                case WriteType.TempOutside:                  
                    //sdAccess.Write(WriteType.TempOutside, temperatur);
                    Settings.Instance.OutsiteTemp.addEntry(DateTime.Now.ToString(), int.Parse(data));
                    pachubeFeedStream = Settings.PachubeFeedStream_OutsiteTemp;               
                    break;

                case WriteType.TempSolarHeater:
                    //sdAccess.Write(WriteType.TempSolarHeater, temperatur);
                    Settings.Instance.SolarHeaterTemp.addEntry(DateTime.Now.ToString(), int.Parse(data));
                    pachubeFeedStream = Settings.PachubeFeedStream_SolarHeaterTemp;

                    break;

                case WriteType.MotorSpeed:
                    //sdAccess.Write(WriteType.TempSolarHeater, temperatur);
                    pachubeFeedStream = Settings.MotorSpeed.ToString();

                    break;
                default:
                    break;           
           }

            if (isNetworkEnabled && pachubeFeedStream != "")
            {
                logToPachube(pachubeFeedStream, data);
            }
             
            
        }

        private void logToPachube(string pachubeFeedStream, string data)
        {
            try
            {
                pachubeClient = new PachubeClient();
                pachubeClient.Send(Settings.PachubeApiKey, Settings.PachubeFeedID, pachubeFeedStream + ", " + data);
                Debug.Print("Data sent to pachube" + DateTime.Now.ToString());
            }
            catch 
            {
                //todo save data in a cache list and/or on SD
                //when network returns send the data
                Error(DateTime.Now + " - error sending data to pachube");
            }
        }

        

    }
}
